/**
 * 
 */
package com.airnews.ticket.ui.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.airnews.ticket.R;
import com.airnews.ticket.ui.BaseActivity;
import com.airnews.ticket.ui.Main;
import com.mobclick.android.MobclickAgent;

/**
 * @author luoxz
 *
 */
public class Logout extends BaseActivity {
	private Button loginOuntButton;
	private TextView userName;
	
	public static String loginName = ""; 
	
    private SharedPreferences sp;
	private String loginNameRe = "";
	private String isLoninOK = null;//是否登录
	private String autoLogin = null;//记着登录
	private Button backBut;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_out_layout);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);

		loginOuntButton =  (Button) findViewById(R.id.button_login_out);
		loginOuntButton.setOnClickListener(onClickListener);
		
		userName = (TextView) findViewById(R.id.userName);
		
		sp = getSharedPreferences("UserInfoB2C", Context.MODE_PRIVATE);
		initConfig();
		
		backBut = (Button) findViewById(R.id.backBut);
		backBut.setOnClickListener(onClickListener);
		
	}
	
	private OnClickListener onClickListener = new OnClickListener(){
		public void onClick(View v) {
			switch(v.getId()){
				case R.id.backBut:
	    			Intent intentR = new Intent();
	    			intentR.setClass(Logout.this, Main.class);
	    			Bundle bundleR=new  Bundle();
	    			bundleR.putString("loginName", loginNameRe);
	    			bundleR.putString("isLoninOK", isLoninOK);
	    			bundleR.putString("autoLogin", autoLogin);
	    			sp.edit().putString("loginName",loginNameRe).commit();
	    			sp.edit().putString("isLoninOK",isLoninOK).commit();
	    			sp.edit().putString("isAutoLogin",autoLogin).commit();
	    			intentR.putExtras(bundleR);
	    			setResult(RESULT_OK, intentR);
	    			finish();
					break;
				case R.id.button_login_out:{
					Intent intent = new Intent();
					intent.setClass(Logout.this, Main.class);
					Bundle bundle=new  Bundle();
	    			bundle.putString("isLoninOK", null);
	    			bundle.putString("isLoninOK", null);
	    			bundle.putString("loginName", "");
	    			SharedPreferences.Editor editor = sp.edit();

	    			editor.remove("userId");
	    			editor.remove("loginName");
	    			editor.remove("isLoninOK");
	    			editor.remove("isAutoLogin");
	    			editor.clear();
	    			editor.commit();
	    			
	    			intent.putExtras(bundle);
	    			setResult(RESULT_OK, intent);
	    			finish();
					break;
				}
				
			}
			
		}
	};
	
    //初始化配置
    private void initConfig() {
     userName.setText(loginName);
     sp = getSharedPreferences("UserInfoB2C", 0);
     loginNameRe = sp.getString("loginName", "");
     isLoninOK = sp.getString("isLoninOK", null);
     autoLogin = sp.getString("isAutoLogin", null);
    }
    
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
